<?php
/**
 * Created by PhpStorm.
 * User: RenBin
 * Date: 2015/12/10
 * Time: 10:44
 */

namespace common\models\Service;


use common\models\DAO\UserRate;
use common\models\DAO\UserRateList;
use common\msg\UserMsg;
use yii\base\Exception;

class RateService {
    public static function init($param){
        $userID = $param['userID'];
        $today = date('Ymd');
        $actID = $param['actID'];
        if($today < $param['begin'])
            return 0;
        if($today >= $param['end'])
            return -1;
      /*  $rateList = UserRateList::find()->where(['date'=>$today])->count();
        if($rateList >= 888)
            return 2;*/

        $userRate = UserRateList::findOne(['date'=>$today, 'userID'=>$userID, 'actID'=>$actID]);
        if($userRate)
            return $userRate->attributes;
        return 1;

    }

    public static function complete($param){
        $userID = $param['userID'];
        $actID = $param['actID'];
        $today = date('Ymd');
        $result['status'] = 0;
        $result['msg'] = UserMsg::$time_out;
        $now = time();
       /* $hour = date('Hi');
        if($today == '20151210' && ($hour < '2030' || $hour > '2230')) {
            $result['msg'] = UserMsg::$cs_no;
            return $result;
        }
        if($today != '20151210' && ($hour < '1200' || $hour > '1400')) {
            $result['msg'] = UserMsg::$cs_no;
            return $result;
        }*/
        if($today < $param['begin']){
            $result['msg'] = UserMsg::$action_no;
            return $result;
        }
        if($today >= $param['end']){
            $result['msg'] = UserMsg::$action_end;
            return $result;
        }
        /*$rateList = UserRateList::find()->where(['date'=>$today])->count();
        if($rateList >= 888){
            $result['msg'] = UserMsg::$coupon_over;
            return $result;
        }*/


        $transaction = \Yii::$app->db->beginTransaction();
        if(!isset($param['rate'])){
            $_param = [
                ['id'=>50, 'chance'=>50],//0.3加息券
                ['id'=>80, 'chance'=>30],//0.8加息券
                ['id'=>100, 'chance'=>20],//1加息券
            ];
            $_result = DrawService::drawservice($_param);
        }else
            $_result = $param['rate'];
        $userRate = new UserRate();
        $userRate->userID = $userID;
        $userRate->rate = $_result;
        $userRate->createTime = $now;
        $rate_day = 7;
        isset($param['day']) && $rate_day = $param['day'];
        $lose_day = $rate_day + 1;
        $userRate->loseTime = date('Ymd', strtotime("+$lose_day day"));
        $userRate->day = $rate_day;
        $userRate->composeID = 0;
        $rate_result = $userRate->save();
        if(!$rate_result){
            $transaction->rollBack();
            return $result;
        }
        $userRateList = new UserRateList();
        try{
            $userRateList->userID = $userID;
            $userRateList->rateID = $userRate->id;
            $userRateList->rate = $_result;
            $userRateList->createTime = $now;
            $userRateList->actID = $actID;
            $userRateList->date = $today;
            $list_result = $userRateList->save();
            if(!$list_result){
                $transaction->rollBack();
                return $result;
            }
        }catch (Exception $e){
            $result['msg'] = UserMsg::$alread_hongbao;
            return $result;
        }
        $transaction->commit();
        return ['status'=>1];
    }
}